/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.magostini.mailservice.sender;

import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 *
 * @author marco
 * 
 */

public class FulFillSender implements Runnable{
     Logger log =Logger.getLogger("INTEGRATION-MAILSERVICE");
   


    private JavaMailSender mailSender;
    private String usernameWishOwner;
    private String emailWishOwner;
    private String urlImage;
    private String text;
    private String link;
    private Long idWish;
    private String usernameFulFill;
    private String emailFulFill;

    private VelocityEngine velocityEngine;

    public FulFillSender(JavaMailSender mailSender, String usernameWishOwner, String emailWishOwner, String urlImage, String text, String link, String usernameFulFill, String emailFulFill,Long idWish, VelocityEngine velocityEngine) {
        this.mailSender = mailSender;
        this.usernameWishOwner = usernameWishOwner;
        this.emailWishOwner = emailWishOwner;
        this.urlImage = urlImage;
        this.text = text;
        this.link = link;
        this.usernameFulFill = usernameFulFill;
        this.emailFulFill = emailFulFill;
        this.idWish = idWish;
        this.velocityEngine = velocityEngine;

    }
    

    

    @Override
    public void run() {

        
        if(this.emailWishOwner!=null){
        MimeMessagePreparator preparatorWishOwner = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {
                        
                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(emailWishOwner);
                        message.setSubject("WaveWish - "+usernameFulFill+ " ti propone un modo per realizzare il tuo desiderio!");
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...
                        
                        Map model = new HashMap();
                        
                        model.put("username", usernameWishOwner);
                        model.put("usernameFulFIll", usernameFulFill);
                        model.put("testo", text);
                        model.put("link", link);
                        model.put("linkPresent", link!=null);
                        model.put("idWish", idWish);
                        model.put("urlImage", urlImage);

                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/fulfillWishOwner.vm", model);
                        log.debug("set Text ");
                        message.setText(text, true);

//                        log.debug("add image ");
//                        message.addInline("imageHeader", new ClassPathResource("/image/Header-mail.jpg"));


                    }
                };

                 try{
                        mailSender.send(preparatorWishOwner);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR FULFILL TO "+emailWishOwner,e);

                    return;
                }
                log.info("EMAIL SEND FOR FULFILL TO "+emailWishOwner);

        }
        if(this.emailFulFill!=null){
          MimeMessagePreparator preparatorFulFill = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {

                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(emailFulFill);
                        message.setSubject("WaveWish - Hai proposto a "+usernameWishOwner+" come esaudire il suo desiderio");
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...

                        Map model = new HashMap();

                        model.put("username", usernameFulFill);
                        model.put("usernameWishOwner", usernameWishOwner);
                        model.put("testo", text);
                        model.put("link", link);
                        model.put("linkPresent", link!=null);
                        model.put("urlImage", urlImage);

                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/fulfill.vm", model);
                        log.debug("set Text ");
                        message.setText(text, true);

//                        log.debug("add image ");
//                        message.addInline("imageHeader", new ClassPathResource("/image/Header-mail.jpg"));


                    }
                };
                log.info("sendig emails ");

               


                 try{
                        mailSender.send(preparatorFulFill);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR FULFILL TO "+emailFulFill,e);

                    return;
                }
                log.info("EMAIL SEND FOR FULFILL TO "+emailFulFill);
    }
    }

}
